package com.example.myfirstgame.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;

import com.example.myfirstgame.R;
import com.example.myfirstgame.controller.GameObject;
import com.example.myfirstgame.helper.Coordinate;
import com.example.myfirstgame.helper.DataInstance;
import com.example.myfirstgame.helper.RasterPoint;

public class Tree implements GameObject {

	// ==================
	// FIELDS
	// ==================

	// unique id
	private static int counter = 0;
	private int treeId = 0;
	private int treeType;
	// position
	private Rect collisionRectangle = null;
	private float currentX;
	private float currentY;
	private Matrix treeMatrix = null;
	// Bitmap
	float mNewWidth;
	float mNewHeight;
	private Bitmap mTreeBitmap = null;
	private static final float mInternalScaleFactor = 1f;
	private static Paint myPaint = new Paint();

	private static Context appContext = null;
	// Final stuff
	public static final String TYPE_DEFINITION = "tree";
	public static final int TYPE_TREE_01 = 0x93d;
	private static final int RESOURCE_TREE_01 = R.drawable.baum;
	public static final int TYPE_TREE_02 = TYPE_TREE_01 + 1;
	private static final int RESOURCE_TREE_02 = R.drawable.baum;

	// ==================
	// CONSTRUCTOR
	// ==================

	public Tree(int _treeType, Context _appContext) {
		this.treeType = _treeType;
		
		if (_appContext != null) {
			appContext = _appContext;
			this.mTreeBitmap = setBitmap(_treeType, appContext);
		}
		
		treeId = counter;
		counter++;
	}
	
	public Tree(int _treeType, Context _appContext, RasterPoint pRasterPoint) {
		this.treeType = _treeType;
		
		setPosition(pRasterPoint.getCoordinate());
		if (_appContext != null) {
			appContext = _appContext;
			this.mTreeBitmap = setBitmap(_treeType, appContext);
			scaleObject(DataInstance.getInstance().getGameObjectScalingFactor(), pRasterPoint.getRasterSize());
		}
		
		treeId = counter;
		counter++;
	}

	public Tree(int _treeType, Context _appContext, int x, int y, int pRasterSize) {
		this.treeType = _treeType;
		this.mTreeBitmap = setBitmap(_treeType, _appContext);
		scaleObject(DataInstance.getInstance().getGameObjectScalingFactor(), pRasterSize);
		if (appContext == null) {
			appContext = _appContext;
		}
		this.currentX = x;
		this.currentY = y;
		treeId = counter;
		counter++;
	}

	// ========================
	// METHODS
	// ========================

	private static Bitmap setBitmap(int carType, Context appContext) {
		Bitmap bitmap = null;
		int res = 0;
		if (carType == TYPE_TREE_01) {
			res = RESOURCE_TREE_01;
		} else if (carType == TYPE_TREE_02) {
			res = RESOURCE_TREE_02;
		}
		if(appContext != null){
			bitmap = BitmapFactory.decodeResource(appContext.getResources(), res);
		}
		return bitmap;
	}

	// ===============================
	// Interface Game Object
	// ===============================

	@Override
	public void setPosition(Coordinate coordinate) {
		this.currentX = coordinate.getX();
		this.currentY = coordinate.getY();
	}

	@Override
	public Coordinate getPosition() {
		return new Coordinate(currentX, currentY);
	}

	private Matrix getDrawMatrix() {
		if (this.treeMatrix == null) {
			this.treeMatrix = new Matrix();
			float lCurrentScaleFactor = DataInstance.getInstance().getGameObjectScalingFactor() * mInternalScaleFactor;
			this.treeMatrix.postScale(lCurrentScaleFactor, lCurrentScaleFactor);
			this.treeMatrix.postTranslate(-mNewWidth / 2, -mNewHeight / 2);
			this.treeMatrix.postTranslate(currentX, currentY);
		}
		return this.treeMatrix;
	}

	@Override
	public String getCollisonType() {
		// TODO Auto-generated method stub
		return TYPE_DEFINITION;
	}

	@Override
	public Rect getBoundingRectangle() {

		if (collisionRectangle == null) {
			// small rectangle for collision detection
			int w = (int) mNewWidth;
			int h = (int) mNewHeight;

			Rect rect = new Rect((int) (currentX - w / 8),
					(int) (currentY - h / 8), (int) (currentX + w / 8),
					(int) (currentY + h / 8));
			collisionRectangle = rect;
			// big rectangle for positioning

			// Rect rect = new Rect((int) (currentX), (int) (currentY),
			// (int) (currentX + w), (int) (currentY + h));
			// collisionRectangle = rect;
		}

		return collisionRectangle;
	}

	@Override
	public String getGameType() {
		return TYPE_DEFINITION;
	}

	@Override
	public void draw(Canvas canvas) {
		canvas.drawBitmap(mTreeBitmap, getDrawMatrix(), null);
		myPaint.setColor(Color.argb(180, 255, 0, 0));
		canvas.drawRect(getBoundingRectangle(), myPaint);
	}

	@Override
	public void scaleObject(float pScaleFactor, int pRasterSize) {
		float lCurrentScaleFactor = pScaleFactor * mInternalScaleFactor;
		treeMatrix = null;
		collisionRectangle = null;
		int w = mTreeBitmap.getWidth();
		int h = mTreeBitmap.getHeight();
		mNewHeight = h * lCurrentScaleFactor;
		mNewHeight = (float)((((int)mNewHeight)/pRasterSize)*pRasterSize);
		mNewWidth = w * lCurrentScaleFactor;
		mNewWidth = (float)((((int)mNewWidth)/pRasterSize)*pRasterSize);
	}

}
